package org.action;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
import org.hibernate.entity.Suggestion;
import org.util.Json;
import org.util.properties.ProperUtil;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * 顶级的ACTION容器;
 * 
 * @Title: BaseAction.java
 * @Description:评师网管理后台(一期)
 * @Copyright: Copyright (c) 2011
 * @Company:北京学邦时代
 * @author 苗有虎
 * @date Mar 13, 2012 10:47:53 AM
 * @version V4.0
 */
@SuppressWarnings("unchecked")
public class BaseAction extends ActionSupport implements ServletContextAware,
		ServletRequestAware, ServletResponseAware {
	private static final long serialVersionUID = 4269261972614854050L;
	public HttpServletRequest request;
	public HttpServletResponse response;
	public ServletContext context;
	public Map session;
	public String baseContext; // 上下文
	public String message; // 提示信息
	public String jumpUrl; // 跳转的路径;
	private ProperUtil util = new ProperUtil();
	public Suggestion suggestion = new Suggestion();

	public Suggestion getSuggestion() {
		return suggestion;
	}

	public void setSuggestion(Suggestion suggestion) {
		this.suggestion = suggestion;
	}

	public String getJumpUrl() {
		return jumpUrl;
	}

	public void setJumpUrl(String jumpUrl) {
		this.jumpUrl = jumpUrl;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public void setServletContext(ServletContext context) {
		this.context = context;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public ServletContext getContext() {
		return context;
	}

	public String getBaseContext() {
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		return basePath;
	}

	/**
	 * 值栈设值;
	 * 
	 * @param name
	 * @param obj
	 */
	public void set(String name, Object obj) {
		ServletActionContext.getValueStack(request).set(name, obj);
	}

	/**
	 * 获得相应的参数值;
	 * 
	 * @param key
	 * @return
	 */
	public int getNumber(String key) {
		ProperUtil util = new ProperUtil();
		return Integer.parseInt(util.getPropertyValue(key));
	}

	/**
	 * 首页头模板;
	 * 
	 * @return
	 */
	public int getIndexHeadTemplate() {
		return Integer.parseInt(util.getPropertyValue("head_template_id"));
	}

	/**
	 * 首页底模板;
	 * 
	 * @return
	 */
	public int getIndexFootTemplate() {
		return Integer.parseInt(util.getPropertyValue("foot_template_id"));
	}

	/**
	 * 首页中间模板;
	 * 
	 * @return
	 */
	public int getIndexMiddleTemplate() {
		return Integer.parseInt(util.getPropertyValue("middle_template_id"));
	}

	/**
	 * 博主主页头模板
	 * 
	 * @return
	 */
	public int getBlogerIndexHeadTemplate() {
		return Integer.parseInt(util
				.getPropertyValue("bloger_head_template_id"));
	}

	/**
	 * 博主主页底模板
	 * 
	 * @return
	 */
	public int getBlogerIndexFootTemplate() {
		return Integer.parseInt(util
				.getPropertyValue("bloger_foot_template_id"));
	}

	/**
	 * 博主主页中间模板
	 * 
	 * @return
	 */
	public int getBlogerIndexMiddleTemplate() {
		return Integer.parseInt(util
				.getPropertyValue("bloger_middle_template_id"));
	}

	/**
	 * 栏目页头模板
	 * 
	 * @return
	 */
	public int getColumnHeadTemplate(int typeId) {
		int result = 0;
		if (typeId == 1) {
			// 新闻类别
			result = Integer.parseInt(util
					.getPropertyValue("news_column_head_template_id"));
		} else if (typeId == 0) {
			// 普通博文
			result = Integer.parseInt(util
					.getPropertyValue("common_article_head_template_id"));
		} else if (typeId == 2) {
			// 专题类别
			result = Integer.parseInt(util
					.getPropertyValue("news_column_head_template_id"));
		} else if (typeId == 3) {
			// 公告类别
			result = Integer.parseInt(util
					.getPropertyValue("news_column_head_template_id"));
		}
		return result;
	}

	/**
	 * 栏目页底模板
	 * 
	 * @return
	 */
	public int getColumnFootTemplate(int typeId) {
		int result = 0;
		if (typeId == 1) {
			// 新闻类别
			result = Integer.parseInt(util
					.getPropertyValue("news_column_foot_template_id"));
		} else if (typeId == 0) {
			// 普通博文
			result = Integer.parseInt(util
					.getPropertyValue("common_article_foot_template_id"));
		} else if (typeId == 2) {
			// 专题类别
			result = Integer.parseInt(util
					.getPropertyValue("news_column_foot_template_id"));
		} else if (typeId == 3) {
			// 公告类别
			result = Integer.parseInt(util
					.getPropertyValue("news_column_foot_template_id"));
		}
		return result;
	}

	/**
	 * 栏目页中间模板
	 * 
	 * @return
	 */
	public int getColumnMiddleTemplate(int typeId) {
		int result = 0;
		if (typeId == 1) {
			// 新闻类别
			result = Integer.parseInt(util
					.getPropertyValue("news_column_middle_template_id"));
		} else if (typeId == 0) {
			// 普通博文
			result = Integer.parseInt(util
					.getPropertyValue("common_article_middle_template_id"));
		} else if (typeId == 2) {
			// 专题类别
			result = Integer.parseInt(util
					.getPropertyValue("news_column_middle_template_id"));
		} else if (typeId == 3) {
			// 公告类别
			result = Integer.parseInt(util
					.getPropertyValue("news_column_middle_template_id"));
		} else if (typeId == 4) {
			// 标签类别
			result = Integer.parseInt(util
					.getPropertyValue("tag_list_middle_template_id"));
		}
		return result;
	}

	/**
	 * 详细头模板
	 * 
	 * @return
	 */
	public int getDetailHeadTemplate(int typeId) {
		int result = 0;
		if (typeId == 1) {
			// 新闻类别
			result = Integer.parseInt(util
					.getPropertyValue("news_detail_head_template_id"));
		} else if (typeId == 0) {
			// 普通博文
			result = Integer.parseInt(util
					.getPropertyValue("article_detail_head_template_id"));
		} else if (typeId == 2) {
			// 专题类别
			result = Integer.parseInt(util
					.getPropertyValue("news_detail_head_template_id"));
		} else if (typeId == 3) {
			// 公告类别
			result = Integer.parseInt(util
					.getPropertyValue("news_detail_head_template_id"));
		}
		return result;
	}

	/**
	 * 详细底模板
	 * 
	 * @return
	 */
	public int getDetailFootTemplate(int typeId) {
		int result = 0;
		if (typeId == 1) {
			// 新闻类别
			result = Integer.parseInt(util
					.getPropertyValue("news_detail_foot_template_id"));
		} else if (typeId == 0) {
			// 普通博文
			result = Integer.parseInt(util
					.getPropertyValue("article_detail_foot_template_id"));
		} else if (typeId == 2) {
			// 专题类别
			result = Integer.parseInt(util
					.getPropertyValue("news_detail_foot_template_id"));
		} else if (typeId == 3) {
			// 公告类别
			result = Integer.parseInt(util
					.getPropertyValue("news_detail_foot_template_id"));
		}
		return result;
	}

	/**
	 * 详细中间模板
	 * 
	 * @return
	 */
	public int getDetailMiddleTemplate(int typeId) {
		int result = 0;
		if (typeId == 1) {
			// 新闻类别
			result = Integer.parseInt(util
					.getPropertyValue("news_detail_middle_template_id"));
		} else if (typeId == 0) {
			// 普通博文
			result = Integer.parseInt(util
					.getPropertyValue("article_detail_middle_template_id"));
		} else if (typeId == 2) {
			// 专题类别
			result = Integer.parseInt(util
					.getPropertyValue("article_detail_middle_template_id"));
		} else if (typeId == 3) {
			// 公告类别
			result = Integer.parseInt(util
					.getPropertyValue("article_detail_middle_template_id"));
		}
		return result;
	}

	/**
	 * 博文资讯类别编号
	 * 
	 * @return
	 */
	public int getTypeNewsId() {
		return Integer.parseInt(util.getPropertyValue("news_type_id"));
	}

	/**
	 * 博文普通类别编号
	 * 
	 * @return
	 */
	public int getTypeAritcleId() {
		return Integer.parseInt(util.getPropertyValue("article_type_id"));
	}

	/**
	 * 博文专题类别编号
	 * 
	 * @return
	 */
	public int getTypeSpecialId() {
		return Integer.parseInt(util.getPropertyValue("special_type_id"));
	}

	/**
	 * 博文公告类别编号
	 * 
	 * @return
	 */
	public int getTypeNoticeId() {
		return Integer.parseInt(util.getPropertyValue("notice_type_id"));
	}
	
	/**
	 * 输出Json对象数据,带编码格式
	 */
	public void jsonOut(Json json, String character) {
		try {
			request.setCharacterEncoding(character);
			response.setCharacterEncoding(character);
			response.setContentType("text/html;charset=" + character);
			PrintWriter out = response.getWriter();
			JSONArray jsonArray = JSONArray.fromObject(json);
			out.println(jsonArray);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
